#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 1e2 + 10, INF = 0x3f3f3f3f;

int n, m;
string s, t;
map<pair<char, char>, int> mp;
int f[N][N];

void init()
{
    mp[{'A', 'A'}] = mp[{'C', 'C'}] = mp[{'G', 'G'}] = mp[{'T', 'T'}] = 5;
    mp[{'A', 'C'}] = mp[{'C', 'A'}] = mp[{'A', 'T'}] = mp[{'T', 'A'}] = mp[{'-', 'T'}] = mp[{'T', '-'}] = -1;
    mp[{'A', 'G'}] = mp[{'G', 'A'}] = mp[{'C', 'T'}] = mp[{'T', 'C'}] = mp[{'G', 'T'}] = mp[{'T', 'G'}] = mp[{'G', '-'}] = mp[{'-', 'G'}] = -2;
    mp[{'A', '-'}] = mp[{'-', 'A'}] = mp[{'C', 'G'}] = mp[{'G', 'C'}] = -3;
    mp[{'C', '-'}] = mp[{'-', 'C'}] = -4;
}

void solve()
{
    init();
    cin >> n >> s >> m >> t;
    s = " " + s;t = " " + t;
    f[0][0] = 0;
    for (int i = 1;i <= n;i++) f[i][0] = f[i - 1][0] + mp[{s[i], '-'}];
    for (int j = 1;j <= m;j++) f[0][j] = f[0][j - 1] + mp[{'-', t[j]}];

    for (int i = 1;i <= n;i++)
        for (int j = 1;j <= m;j++) {
            f[i][j] = -INF;
            f[i][j] = max(f[i][j], f[i - 1][j - 1] + mp[{s[i], t[j]}]);
            f[i][j] = max(f[i][j], f[i - 1][j] + mp[{s[i], '-'}]);
            f[i][j] = max(f[i][j], f[i][j - 1] + mp[{'-', t[j]}]);
        }
    cout << f[n][m];
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}